local ansha = fk.CreateSkill{
  name = "dajubei__ansha",
}

Fk:loadTranslationTable{
  ["dajubei__ansha"] = "暗煞",
  [":dajubei__ansha"] = "锁定技，准备阶段，你从死亡骑士牌堆中发现一张牌；死亡骑士牌进入弃牌堆时，改为洗回死亡骑士牌堆。",

  ["ansha_death_knights"] = "死亡骑士",

  ["$dajubei__ansha1"] = "巫妖王给了我真正的力量！",
  ["$dajubei__ansha2"] = "这个“黑暗”到底是谁？",
}

local names = {"frostmourne", "army_of_the_dead", "doom_pact", "death_grip", "death_coil", "obliterate", "anti_magic_shell", "death_and_decay"}

local death_knights_list = {
  {"frostmourne", Card.Spade, 5},
  {"army_of_the_dead", Card.Spade, 6},
  {"doom_pact", Card.Spade, 5},
  {"death_grip", Card.Spade, 2},
  {"death_coil", Card.Spade, 2},
  {"obliterate", Card.Spade, 2},
  {"anti_magic_shell", Card.Spade, 4},
  {"death_and_decay", Card.Spade, 3},
}

ansha:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local death_knights = {}
  table.forEach(death_knights_list, function (t)
    local card = table.find(room.void, function (id)
      return Fk:getCardById(id).name == t[1]
    end) or room:printCard(t[1], t[2], t[3]).id
    table.insert(death_knights, card)
  end)

  if #death_knights > 0 then
    player:addToPile("ansha_death_knights", death_knights)
  end
end)

ansha:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(ansha.name) and player == target and data.phase == Player.Start
    and #player:getPile("ansha_death_knights") > 0
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player:getPile("ansha_death_knights")
    cards = table.random(cards, math.min(#cards, 3))
    local card = player.room:askToChooseCard(player, {
      target = player,
      flag = { card_data = {{"ansha_death_knights", cards},} },
      skill_name = ansha.name,
    })

    if card then
      event:setCostData(self, card)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local card = event:getCostData(self)
    player.room:obtainCard(player, card, true, fk.ReasonJustMove, player, ansha.name)
  end
})

ansha:addEffect(fk.AfterCardsMove, {
  anim_type = "special",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(ansha.name) then
      local cards = {}
      table.forEach(data, function (move)
        if move.toArea == Card.DiscardPile then
          table.forEach(move.moveInfo, function (info)
            if table.contains(names, Fk:getCardById(info.cardId).name) then
              table.insert(cards, info.cardId)
            end
          end)
        end
      end)
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self) ---@type integer[]
    player:addToPile("ansha_death_knights", cards, true, ansha.name, player)
  end
})

return ansha